package cn.getword.cnblogs.controller;


import cn.getword.cnblogs.dao.HomeDao;
import cn.getword.cnblogs.dao.UserDao;
import cn.getword.cnblogs.entity.Page;
import cn.getword.cnblogs.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HomeController {
    @Autowired
    private HomeDao homeDao;
    @Autowired
    private UserDao userDao;

    private Logger logger = LoggerFactory.getLogger(HomeController.class);

    @RequestMapping({"", "/index"})
    public String index(Page page) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", "123");
        List<User> list2 = userDao.findAll(page, map);
        return "index";
    }

    @RequestMapping("/home")
    public String home() {
        return "home";
    }

    @RequestMapping("/newNote")
    public String newNote(){
        return "newNote";
    }

    /**
     * @Description //TODO 上传图片
     * @Author zhuxiang
     * @Date 17:06 2020/4/16
     * @Param [file, request]
     * @return java.lang.Object
     **/
    @RequestMapping("/uploadClientFile")
    @ResponseBody
    public Object uploadClientFile(MultipartFile file, HttpServletRequest request) throws IOException, ServletException {
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> files = multiRequest.getFileMap();
        for (MultipartFile multipartFile : files.values()) {
            if (multipartFile.isEmpty()) {
                continue;
            }
            String fileName = multipartFile.getOriginalFilename();
            if (fileName == null || fileName.trim().equals("")) {
                continue;
            }
            Integer index = fileName.lastIndexOf("\\");
            String newStr = "";
            if (index > -1) {
                newStr = fileName.substring(index + 1);
            } else {
                newStr = fileName;
            }
            if (!newStr.equals("")) {
                fileName = newStr;
            }
            String realPath = request.getSession().getServletContext().getRealPath("/static/client");
            File upload = new File(realPath);
            if (!upload.exists()) {
                upload.mkdirs();
            }
            File dest = new File(upload.getAbsolutePath() + "/" + fileName);
            if (multipartFile != null) {
                multipartFile.transferTo(dest);
            }
            Map<String, Object> result = new HashMap<>();
            result.put("success", "1");
            result.put("message", "成功！");
            result.put("url", "/static/client/" + fileName);
            return result;
        }
        return null;
    }
}
